const {
  hot,
  past
} = require('../../data/search.js');
const { newNav, goodsList} = require('../../data/goods.js')

// miniprogram/pages/search/search.js
module.exports = require('../../data/search.js')
module.exports = require('../../data/goods.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    avtiveNavIndex:0,
    reveal:false,
    containerFlag:false,
    pageFlag:true,
    inputValue: '',
    searchNav:[],
    wareList:[],
    allWares:[],
    Hot: [],
    Past: [],
    searchRecord: []
  },
  openHistorySearch: function () {
    this.setData({
      searchRecord: wx.getStorageSync('searchRecord') || [], //若无储存则为空
    })
  },
  doSearch(e) {
    wx.showToast({
      title: this.data.inputValue
    });
    // console.log(e);
    var that = this
    var inputVal = e.detail.value
    var searchRecord = this.data.searchRecord
    if (inputVal == '') {
      containerFlag:false;
      pageFlag:true;
      //输入为空时的处理
    } else {
      //将搜索值放入历史记录中,只能放前15条
      if (searchRecord.length < 10) {
        searchRecord.unshift({
          value: inputVal,
          id: searchRecord.length
        })
      } else {
        searchRecord.pop() //删掉旧的时间最早的第一条
        searchRecord.unshift({
          value: inputVal,
          id: searchRecord.length
        })
      }   
    }    
    wx.setStorageSync('searchRecord', searchRecord);
    this.setData({
      searchRecord: searchRecord,
      containerFlag:true,
      pageFlag:false
    })
  },
  pastDelete(){
    this.popup2.showPopup();
  },
  _success(){
    wx.clearStorageSync("searchRecord");
    this.setData({
      searchRecord:[]
    })
    this.popup2.hidePopup()
  },
  _error(){
    //父组件得到消息，子组件error点击了
    this.popup2.hidePopup()
},
  intoPast(e){
    // console.log(e);
    let { text } = e.currentTarget.dataset;
    this.setData({
      inputValue:text,
      containerFlag:true,
      pageFlag:false
    });
    var searchRecord = this.data.searchRecord;
    
    if(searchRecord.length < 10 ){
      searchRecord.unshift({
        value:text
      })   
    }else{
      searchRecord.pop();
      searchRecord.unshift({
        value:text
      })   
    };
    wx.setStorageSync('searchRecord', searchRecord);
    this.setData({
      searchRecord: searchRecord,
    });
  },
  gntoPast(e){
    console.log(e);
  },
  clearInput() {
    this.setData({
      inputValue: '',
      containerFlag:false,
      pageFlag:true
    })
  },
  inputTyping(event) {
    this.setData({
      inputValue: event.detail.value
    })
  },
  changeTabs(e){
    // console.log(e);
    let { id, index } = e.currentTarget.dataset;
    this.setData({
      avtiveNavIndex:index
    });
    let wares = this.data.allWares.filter(ware => (ware.num-1) == id);
    this.setData({
      wareList:wares
    })
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.openHistorySearch();
    this.setData({
      Hot: hot,
      Past: past,
      searchNav:newNav,
      wareList:goodsList,
      allWares:goodsList
    })
    this.popup2 = this.selectComponent('#popup2');
    let fwares = this.data.allWares.filter(ware => (ware.num-1) == 0);
    this.setData({
      wareList:fwares
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})